# -*- conding:UTF-8 -*-

'''
@ Project: model
@ File: users.py
@ Author: 韩成瑾
@ Date：2022/8/10 19:16

'''


from flask import Blueprint, jsonify, request
from flask_restful import Api, Resource, reqparse,marshal_with,fields,marshal
from qiniu import Auth,put_file
import random
from models.model import *

user_bp = Blueprint("user_bp",__name__,url_prefix="/")

api = Api(user_bp)

# 登陆注册
class UserView(Resource):
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('mobile')
        req.add_argument('code')
        args = req.parse_args()

        user_info = User.query.filter(id==User.id).first()
        if not user_info:
            user0 = User(name=args['name'],mobile=args['mobile'])
            db.session.add(user0)
            db.session.commit()

        user_info = User.query.filter(id == User.id).first()
        if user_info.mobile!=args['mobile'] or user_info.code!=args['code']:
            return '账号密码错误'
        else:
            return jsonify({
                'code':200,
                'msg':'登陆成功',
                'user':{
                    'id':user_info.id,
                    'name':user_info.name,
                    'img':user_info.img,
                    'mobile':user_info.mobile
                }
            })

    def get(self):
        req = reqparse.RequestParser()
        req.add_argument('id')
        args = req.parse_args()

        user_all_info = User.query.filter(id==args['id']).all()

        user_info_list = []
        for i in user_all_info:
            a = {
                'id':i.id,
                    'name':i.name,
                    'img':i.img,
                    'mobile':i.mobile
            }
            user_info_list.append(a)

        return jsonify({
            'code':200,
            'list':user_info_list
        })




# 好友
class FriendsView(Resource):
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('img')
        req.add_argument('group')
        args = req.parse_args()

        if User.name==args['name']:
            return jsonify({'msg':'已有该好友'})
        else:
            db.session.add(args)
            db.session.commit()

        return jsonify({'code':200,'msg':'添加成功'})


    def get(self):
        req = reqparse.RequestParser()
        req.add_argument('id')
        args = req.parse_args()

        user_info = User.query.filter(id=args['id']).first()
        friend_info = user_info.query.filter(id=user_info.id).all()

        friends_info_list = []

        for friend in friend_info:
            f = {
                'id':friend.id,
                'name':friend.name,
                'img':friend.img,
                'group':friend.group
            }
            friends_info_list.append(f)

            return jsonify({
                'code':200,
                'list':friends_info_list
            })



class FriendView(Resource):
    def put(self,id):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('group')
        args = req.parse_args()

        Friends.query.filter(Friends.id==id).one().update(args)
        db.session.commit()
        return jsonify({
            'code':200,'msg':'修改成功'
        })

    def delete(self,id):
        Friends.query.filter(Friends.id==id).delete()
        db.session.commit()
        return jsonify({
            'code':200,'msg':'删除成功'
        })



# 朋友圈
class CicrleView(Resource):
    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('contnet')
        req.add_argument('img')
        req.add_argument('backref')
        args = req.parse_args()

        if int('content')>50:
            return jsonify({'code':403,'msg':'字数太多啦'})

        db.session.add(args)
        db.session.commit()

        return jsonify({
            'code':200,'msg':'发表成功'
        })

    def get(self):
        req = reqparse.RequestParser()





# 用户
api.add_resource(UserView,'user')
# 朋友
api.add_resource(FriendsView,'friends')
api.add_resource(FriendView,'friend/<int:id>')
# 朋友圈
api.add_resource(CicrleView,'cicrle')

